from flask import Flask
from flask_cors import CORS
from dotenv import load_dotenv
import os
import sys

def create_app():
    # 解决Windows下Python输出缓冲问题
    if sys.platform == "win32":
        # 在Windows环境下禁用输出缓冲
        import builtins
        # 重写print函数，添加flush=True参数
        original_print = builtins.print
        def print(*args, **kwargs):
            kwargs.setdefault('flush', True)
            return original_print(*args, **kwargs)
        builtins.print = print
    
    load_dotenv()
    app = Flask(__name__)
    app.config["SECRET_KEY"] = os.getenv("SECRET_KEY", "dev")
    app.config["MONGO_URI"] = os.getenv("MONGO_URI", "mongodb://localhost:27017/NeuInk")
    app.config["API_PREFIX"] = os.getenv("API_PREFIX", "/api/v1")
    
    # 配置日志，确保在Windows环境下也能立即输出
    import logging
    
    # 设置日志级别
    log_level = os.getenv("LOG_LEVEL", "INFO").upper()
    app.logger.setLevel(getattr(logging, log_level))
    
    # 添加StreamHandler，确保日志立即输出
    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(getattr(logging, log_level))
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    app.logger.addHandler(handler)
    
    # 在Windows环境下确保日志不被缓冲
    if sys.platform == "win32":
        # 设置流为行缓冲模式
        if hasattr(handler.stream, 'reconfigure'):
            handler.stream.reconfigure(line_buffering=True)
        else:
            # 对于不支持reconfigure的Python版本，设置缓冲区大小为1
            import io
            handler.stream = io.TextIOWrapper(
                handler.stream.buffer,
                encoding=handler.stream.encoding,
                newline=None,
                line_buffering=True
            )

    CORS(app, resources={
        r"/*": {
            "origins": [
                "http://localhost:3000",
                "http://127.0.0.1:3000",
                "http://localhost:3001",
                "http://localhost:3002",
                "http://127.0.0.1:3002",
                "http://localhost:8000",
                "http://127.0.0.1:8000",
            ],
            "methods": ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
            "allow_headers": "*",
            "expose_headers": [
                "Content-Type",
                "Authorization",
                "X-Total-Count",
                "Cache-Control",
                "Connection"
            ],
            "supports_credentials": True
        }
    })

    prefix = app.config["API_PREFIX"]

    # 注册基础模块
    from neuink.routes.health import bp as health_bp
    from neuink.routes.users import bp as users_bp
    from neuink.routes import notes
    app.register_blueprint(health_bp, url_prefix=f"{prefix}/health")
    app.register_blueprint(users_bp, url_prefix=f"{prefix}/users")
    app.register_blueprint(notes.bp, url_prefix=f"{prefix}/notes")

    # 注册论文相关蓝图
    from neuink.routes import init_app as init_paper_routes
    init_paper_routes(app, prefix)

    @app.before_request
    def log_request():
        from flask import request
        print(f"[IN] {request.method} {request.path}")
        payload = request.get_json(silent=True)
        if payload is not None:
            print(f"   Body: {payload}")

    @app.after_request
    def log_response(response):
        from flask import request
        print(f"[OUT] {request.method} {request.path} -> {response.status_code}")
        return response

    return app
